package com.sudo.controller;

import com.sudo.aspect.AuthorizationApi;
import com.sudo.common.enums.SysStatus;
import com.sudo.common.enums.UserParams;
import com.sudo.common.utils.JsonRes;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.pojo.SysButton;
import com.sudo.pojo.SysMenu;
import com.sudo.pojo.SysRole;
import com.sudo.pojo.SysRoleMenu;
import com.sudo.pojo.bo.SysRoleBO;
import com.sudo.service.SysButtonService;
import com.sudo.service.SysMenuService;
import com.sudo.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @author：为道日损 2020-04-25 23:32
 */
@Api(value = "角色接口",tags = {"角色相关的接口"})
@RestController
@RequestMapping("sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @AuthorizationApi(menu = "roleRun",button = "search")
    @GetMapping("querySysRoleByPage")
    @ApiOperation(value = "分页查找角色",notes = "后台查找角色分页进行管理",httpMethod = "GET")
    public JsonRes querySysRoleByPage(@RequestParam Integer page,
                                      @RequestParam Integer pageSize,
                                      @RequestParam String roleCodeOrName) {
        PagedGridResult gridResult = sysRoleService.querySysRoleByPage(page, pageSize,roleCodeOrName);
        return JsonRes.ok(gridResult);
    }

    @AuthorizationApi(menu = "roleRun",button = "search")
    @GetMapping("getSysRoleByRoleCode")
    @ApiOperation(value = "查找一个角色",notes = "查找角色进行管理，查看或修改",httpMethod = "GET")
    public JsonRes getSysRoleByRoleCode(@RequestParam String roleCode) {
        SysRole sysRole = sysRoleService.getSysRoleByRoleCode(roleCode);
        return JsonRes.ok(sysRole);
    }

    @AuthorizationApi(menu = "roleRun",button = "update")
    @PostMapping("updateSysRoleByCode")
    @ApiOperation(value = "修改一个角色",notes = "根据角色code修改当前角色",httpMethod = "POST")
    public JsonRes updateSysRoleByCode(@RequestBody @Valid SysRoleBO sysRoleBO,
                                     BindingResult result,
                                     HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        String sessionUser = getSessionUser(request);

        if (superAdmin(sysRoleBO.getRoleCode())) {
            return JsonRes.errorMsg("禁止修改超级管理员");
        }
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        Integer affectCount = sysRoleService.updateSysRoleByCode(sessionUser ,sysRoleBO);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "roleRun",button = "delete")
    @PostMapping("deleteSysRoleByCode")
    @ApiOperation(value = "删除一个角色",notes = "根据角色id删除当前角色",httpMethod = "POST")
    public JsonRes deleteSysRoleByCode(@RequestParam("roleCode") String roleCode,
                                     HttpServletRequest request) {
        String sessionUser = getSessionUser(request);
        if (superAdmin(roleCode)) {
            return JsonRes.errorMsg("禁止删除超级管理员");
        }
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        Integer userCount = sysRoleService.getUserCountByRoleCode(roleCode);
        if (userCount > 0) {
            return JsonRes.errorMsg("该角色下尚有" + userCount + "个用户，请先移除用户");
        }
        Integer affectCount = sysRoleService.deleteSysRoleByRoleCode(roleCode);
        return JsonRes.ok(affectCount);
    }

    @AuthorizationApi(menu = "roleRun",button = "search")
    @GetMapping("getAllSysRoleCodeAndNameByStatus")
    @ApiOperation(value = "获取所有角色名称和代码",notes = "获取所有角色名称和代码",httpMethod = "GET")
    public JsonRes getAllSysRoleCodeAndNameByStatus(@RequestParam("status") String status) {
        List<SysRole> sysRole = sysRoleService.getAllSysRoleCodeAndNameByStatus(status);
        return JsonRes.ok(sysRole);
    }

    @AuthorizationApi(menu = "roleRun",button = "search")
    @GetMapping("getSimpleRoleByPage")
    @ApiOperation(value = "分页获取所有角色简单信息",notes = "分页获取所有角色简单信息",httpMethod = "GET")
    public JsonRes getSimpleRoleByPage(@RequestParam Integer page,
                                       @RequestParam Integer pageSize,
                                       @RequestParam String status) {
        PagedGridResult gridResult = sysRoleService.getSimpleRoleByPage(page,pageSize,status);
        return JsonRes.ok(gridResult);
    }

    @AuthorizationApi(menu = "roleRun",button = "add")
    @PostMapping("addSysRole")
    @ApiOperation(value = "添加角色",notes = "添加角色",httpMethod = "POST")
    public JsonRes addSysRole(@RequestBody @Valid SysRoleBO sysRoleBO,
                              BindingResult result,
                              HttpServletRequest request) {
        if (result.hasErrors()) {
            return JsonRes.errorMsg(getFirstError(result));
        }
        String sessionUser = getSessionUser(request);
        if (!superName(sessionUser)) {
            return JsonRes.errorMsg("只允许超级管理员操作");
        }
        //判断角色code是否已经存在
        SysRole oldSysRole = sysRoleService.getSysRoleByRoleCode(sysRoleBO.getRoleCode());
        if (null != oldSysRole) {
            return JsonRes.errorMsg("角色代码已存在，请重新添加");
        }
        int affectCount = sysRoleService.addSysRole(sysRoleBO,sessionUser);
        return JsonRes.ok(affectCount);
    }
}
